package com.cyy.view

import javafx.scene.control.TreeItem
import tornadofx.*
import java.time.LocalDate
import java.time.Period

class Search : View("My View") {
    override val root = borderpane {
        paddingAll=10
        left {
            val persons = listOf(
                    Person1("Mary Hanes", "Marketing"),
                    Person1("Steve Folley", "Customer Service"),
                    Person1("John Ramsy", "IT Help Desk"),
                    Person1("Erlick Foyes", "Customer Service"),
                    Person1("Erin James", "Marketing"),
                    Person1("Jacob Mays", "IT Help Desk"),
                    Person1("Larry Cable", "Customer Service")
            )
            val departments = persons
                    .map { it.department }
                    .distinct().map { Person1(it, "") }

            treeview<Person1> {
                // Create root item
                root = TreeItem(Person1("Departments", ""))

                // Make sure the text in each TreeItem is the name of the Person
                cellFormat { text = it.name }

                // Generate items. Children of the root item will contain departments
                populate { parent ->
                    if (parent == root) departments else persons.filter { it.department == parent.value.name }
                }
            }
        }
        center {
            val persons = listOf(
                    Person(1, "Samantha Stuart", LocalDate.of(1981, 12, 4)),
                    Person(2, "Tom Marks", LocalDate.of(2001, 1, 23)),
                    Person(3, "Stuart Gills", LocalDate.of(1989, 5, 23)),
                    Person(3, "Nicole Williams", LocalDate.of(1998, 8, 11))
            ).asObservable()
            tableview(persons) {
                readonlyColumn("ID", Person::id)
                readonlyColumn("Name", Person::name)
                readonlyColumn("Birthday", Person::birthday)
                readonlyColumn("Age", Person::age)
            }
        }
    }
}

class Person(val id: Int, val name: String, val birthday: LocalDate) {
    val age: Int get() = Period.between(birthday, LocalDate.now()).years
}

data class Person1(val name: String, val department: String)
